package com.yuexin.project.statistic.homeReport.controller;


import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuexin.common.utils.DayUtils;
import com.yuexin.common.utils.security.ShiroUtils;
import com.yuexin.framework.web.controller.BaseController;
import com.yuexin.framework.web.domain.AjaxResult;
import com.yuexin.project.statistic.everyday.domain.Everyday;
import com.yuexin.project.statistic.everyday.mapper.EverydayMapper;

/**
 * 首页报表 信息操作处理
 * 
 * @author yuexin
 * @date 2018-10-14
 */
@Controller
@RequestMapping("/statistic/homeReport")
public class HomeReportController extends BaseController
{
    private String prefix = "statistic/homeReport";
    
    @Autowired
	private EverydayMapper everydayMapper;
	
	@RequiresPermissions("statistic:homeReport:view")
	@GetMapping()
	public String book()
	{
	    return prefix + "/homeReport";
	}
	
	public static void main(String[] args) {
		 /*Calendar cal = Calendar.getInstance(); 
	        cal.setTime(new Date()); 
	        cal.set(Calendar.DAY_OF_MONTH, 1); 
	        System.out.println (new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime())); 
	        cal.roll(Calendar.DAY_OF_MONTH, -1);  
	        System.out.println (new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));*/
		
		/*Date date=new Date();//取时间
		 Calendar calendar = new GregorianCalendar();
		 calendar.setTime(date);
		 calendar.add(calendar.DATE,0);//把日期往后增加一天.整数往后推,负数往前移动
		 date=calendar.getTime(); //这个时间就是日期往后推一天的结果 
		 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		 String dateString = formatter.format(date);
		  
		 System.out.println(dateString);*/
		
		/*try {
			System.out.println(DayUtils.IsToday("2019-01-09"));
			System.out.println(DayUtils.IsTomorrowday("2019-01-10"));
		} catch (ParseException e) {
			e.printStackTrace();
		}*/
		/*int a = 28;
		int b = 29;
		
	    DecimalFormat df=new DecimalFormat("0");//设置保留位数
	    
		//System.out.println(df.format((float)a/b));
		
		System.out.println(df.format(3.41));*/
	}
	
	/**
	 * 查询人员报餐统计列表
	 */
	@GetMapping("/list")
	@ResponseBody
	public AjaxResult list(Everyday everyday)
	{
		List<Object> result = new ArrayList<Object>();
		
		Calendar cal = Calendar.getInstance(); 
        cal.setTime(new Date()); 
        cal.set(Calendar.DAY_OF_MONTH, 1); 
        String beginTime = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        cal.roll(Calendar.DAY_OF_MONTH, -1);  
        String endTime = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("beginTime", beginTime);
		params.put("endTime", endTime);
		everyday.setParams(params);
		everyday.setCompanyId(ShiroUtils.getCompanyId());
        List<Everyday> list = everydayMapper.selectEverydayCountListForReport(everyday);
        List<String> dateList = new ArrayList<String>();
        List<String> breakfastList = new ArrayList<String>();
        List<String> lunchList = new ArrayList<String>();
        List<String> dinnerList = new ArrayList<String>();
        List<String> otherList = Lists.newArrayList();
        Map<String, Object> sta = new HashMap<String, Object>();
        int i = 0;
        int breakfastCount = 0;
        int lunchCount = 0;
        int dinnerCount = 0;
        int otherCount = 0;
        for(Everyday every : list) {
        	i++;
        	String staDate = every.getStaDate();
        	String breakfast = every.getBreakfastCount();
        	String lunch = every.getLunchCount();
        	String dinner = every.getDinnerCount();
        	String other = every.getOtherCount();
        	dateList.add(staDate);
        	breakfastList.add(breakfast);
        	lunchList.add(lunch);
        	dinnerList.add(dinner);
        	otherList.add(other);
        	breakfastCount += Integer.parseInt(breakfast);
        	lunchCount += Integer.parseInt(lunch);
        	dinnerCount += Integer.parseInt(dinner);
        	otherCount += Integer.parseInt(other);
        	try {
				if(DayUtils.IsToday(staDate)) {
					sta.put("todayBreakfast", breakfast);
					sta.put("todayLunch", lunch);
					sta.put("todayDinner", dinner);
					sta.put("todayOther", other);
				}
				
				if(DayUtils.IsTomorrowday(staDate)) {
					sta.put("tomorrowBreakfast", breakfast);
					sta.put("tomorrowLunch", lunch);
					sta.put("tomorrowDinner", dinner);
					sta.put("tomorrowOther", other);
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
        }
        
        if(i>0) {
        	//DecimalFormat df=new DecimalFormat("0.00");//设置保留位数
        	DecimalFormat df=new DecimalFormat("0");
        	sta.put("averBreakfast", df.format((float)breakfastCount/i));
            sta.put("averLunch", df.format((float)lunchCount/i));
            sta.put("averDinner", df.format((float)dinnerCount/i));
            sta.put("averOther", df.format((float)otherCount/i));
        }
        
        result.add(dateList);
        result.add(breakfastList);
        result.add(lunchList);
        result.add(dinnerList);
        result.add(otherList);
        result.add(sta);
		return success(result);
	}
}
